//
//  StepperView.m
//  TextToImg
//
//  Created by Garenge on 2019/M/29.
//  Copyright © 2019 鹏鹏. All rights reserved.
//

#import "StepperView.h"
#import <Masonry/Masonry.h>
#import "UILabel+text.h"

@interface StepperView()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textField;

// 防止暴力点击
@property (nonatomic,assign) BOOL hasLiked;
@end

@implementation StepperView

NSString *integerToString(NSInteger integer) {
    return [NSString stringWithFormat:@"%ld", (long)integer];
}

- (NSInteger)stepValue {
    return 1;
}

- (NSInteger)maxValue {
    if (_maxValue > 0) {
        return _maxValue;
    } else {
        _maxValue = 0;
        return _maxValue;
    }
}

- (NSInteger)minValue {
    if (_minValue > 0) {
        return _minValue;
    } else {
        _minValue = 0;
        return _minValue;
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
    CGFloat width = [self.titleLabel textWidth:title] + 2;
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
}

- (void)setValue:(NSInteger)value {
    _value = value;
    if (value > self.maxValue) {
        _value = self.maxValue;
    }
    if (value < self.minValue) {
        _value = self.minValue;
    }
    self.textField.text = integerToString(_value);
}

- (instancetype)init {
    if (self = [super init]) {
        self.hasLiked = YES;
        
        // 左边创建一个label title
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = @"";
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(0);
            make.centerY.equalTo(self);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(50);
        }];
        
        // 右边是 按钮 + textField + 按钮的布局
        // 最右边的加号
        UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [plusButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [plusButton setTitle:@"+" forState:UIControlStateNormal];
        [plusButton addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:plusButton];
        
        [plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(40, 25));
        }];
        
        // 中间的textField
        UITextField *textField = [[UITextField alloc] init];
        textField.textAlignment = NSTextAlignmentCenter;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.font = [UIFont systemFontOfSize:15];
        textField.text = @"";
        [self addSubview:textField];
        self.textField = textField;
        
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(plusButton.mas_left).with.offset(-2);
            make.centerY.equalTo(plusButton);
            make.size.mas_equalTo(CGSizeMake(40, 25));
        }];
        
        // 左边的减号
        UIButton *reduceButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [reduceButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [reduceButton setTitle:@"-" forState:UIControlStateNormal];
        [reduceButton addTarget:self action:@selector(reduceButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:reduceButton];
        
        [reduceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(textField.mas_left).with.offset(-2);
            make.centerY.equalTo(plusButton);
            make.size.mas_equalTo(CGSizeMake(40, 25));
        }];
    }
    return self;
}

#pragma mark 加
- (void)plusButtonClick:(UIButton *)sender {
    
    if ([self canIClickButton:sender]) {
        self.value = self.value + 1;
        self.textField.text = integerToString(self.value);
        if (self.operationBlock) {
            self.operationBlock(self.value);
        }
    }
}
#pragma mark 减
- (void)reduceButtonClick:(UIButton *)sender {
    if ([self canIClickButton:sender]) {
        self.value = self.value - 1;
        self.textField.text = integerToString(self.value);
        if (self.operationBlock) {
            self.operationBlock(self.value);
        }
    }
}

- (BOOL)canIClickButton:(UIButton *)button {
    __weak typeof(self) weakSelf = self;

    if (self.hasLiked == YES) { // 点击的时候判断hasLiked
        self.hasLiked = NO;// 执行弹框前设置
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.hasLiked = YES; //弹框完成后延时0.25秒在设置
        });
        return YES;
    } else {
        return NO;
    }
}

@end
